package com.yss.kettle.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobParam;
import com.yss.kettle.service.IJobParamService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * kettle参数信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/param")
public class JobParamController extends BaseController {
    @Autowired
    private IJobParamService jobParamService;

    /**
     * 查询kettle参数信息列表
     */
    @RequiresPermissions("kettle:param:list")
    @GetMapping("/list")
    public TableDataInfo list(JobParam jobParam) {
        startPage();
        List<JobParam> list = jobParamService.selectJobParamList(jobParam);
        return getDataTable(list);
    }

    /**
     * 导出kettle参数信息列表
     */
    @RequiresPermissions("kettle:param:export")
    @Log(title = "kettle参数信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobParam jobParam) {
        List<JobParam> list = jobParamService.selectJobParamList(jobParam);
        EasyExeclUtil.export(response, list, JobParam.class);
    }

    /**
     * 获取kettle参数信息详细信息
     */
    @RequiresPermissions("kettle:param:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(jobParamService.getById(id));
    }

    /**
     * 新增kettle参数信息
     */
    @RequiresPermissions("kettle:param:add")
    @Log(title = "kettle参数信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobParam jobParam) {
        jobParam.setId(IdUtils.fastSimpleUUID());
        jobParam.setCreateBy(SecurityUtils.getUsername());
        jobParam.setCreateTime(new Date());
        if (!jobParamService.checkJobParamKeyNameUnique(jobParam)) {
            return error("英文名称【" + jobParam.getKeyName() + "】的参数信息已存在");
        }
        return toAjax(jobParamService.save(jobParam));
    }

    /**
     * 修改kettle参数信息
     */
    @RequiresPermissions("kettle:param:edit")
    @Log(title = "kettle参数信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobParam jobParam) {
        jobParam.setUpdateTime(new Date());
        jobParam.setUpdateBy(SecurityUtils.getUsername());
        if (!jobParamService.checkJobParamKeyNameUnique(jobParam)) {
            return error("英文名称【" + jobParam.getKeyName() + "】的参数信息已存在");
        }
        return toAjax(jobParamService.updateById(jobParam));
    }

    /**
     * 删除kettle参数信息
     */
    @RequiresPermissions("kettle:param:remove")
    @Log(title = "kettle参数信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobParamService.removeByIds(ids));
    }
}
